package com.ruoyi.file.config;

import java.io.File;

import com.ruoyi.common.core.utils.OSUtils;
import com.ruoyi.file.utils.FileUploadUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 通用映射配置
 *
 * @author ruoyi
 */
@Configuration
public class ResourcesConfig implements WebMvcConfigurer
{
    /**
     * 上传文件存储在本地的根路径
     */
    @Value("${file.linuxPath}")
    private String localFileLinuxPath;
    @Value("${file.winPath}")
    private String localFileWinPath;

    /**
     * 资源映射路径 前缀
     */
    @Value("${file.prefix}")
    public String localFilePrefix;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry)
    {
        /** 本地文件上传路径 */
        if(OSUtils.isWindows()){
            registry.addResourceHandler(localFilePrefix + "/**")
                    .addResourceLocations("file:" + localFileWinPath + File.separator);
        }else{
            registry.addResourceHandler(localFilePrefix + "/**")
                    .addResourceLocations("file:" + localFileLinuxPath + File.separator);
        }


    }

    /**
     * 开启跨域
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 设置允许跨域的路由
        registry.addMapping(localFilePrefix  + "/**")
                // 设置允许跨域请求的域名
                .allowedOrigins("*")
                // 设置允许的方法
                .allowedMethods("GET");
    }
}
